package{
public dynamic class JsonArray extends Array{
	//Array inside json can contain another array or json
	public function JsonArray(str:String){
		//the str should not contain \v, because it's used as a delimiter
		super();
		//cleaning
		str = su.trim(str);
		if(str.charAt(0) == "[" && str.charAt(str.length-1) == "]"){
			//remove beginning and ending [] if any
			str = str.substring(1,str.length-1);
			str = su.trim(str);
		}
		//split the elements
		var cBracket:Number = 0, cCurly:Number = 0;
		for(i=0;i<str.length;i++){
			var c:String = str.charAt(i);
			if(c == "[") cBracket++;
			else if(c == "]") cBracket--;
			else if(c == "{") cCurly++;
			else if(c == "}") cCurly--;
			if(c == "," && commaIsOutsideQuote(str,i) && cBracket == 0 && cCurly == 0){
				//this is comma that's not inside a bracket/curly/quote
				//temporary replace the delimiter
				str = su.setCharAt(str,i,"\v");
			}
		}
		var el:Array = su.splitT(str,"\v");
		for(var i=0;i<el.length;i++){
			var content:String = el[i];
			var c:String = content.charAt(0);
			if(c == "["){
				//it's array
				this[i] = new JsonArray(content);
			}else if(c == "{"){
				//it's json
				this[i] = new Json(content);
			}else{
				//string or number
				this[i] = su.getData(content);
			}
		}
	}
	function commaIsOutsideQuote(str:String,index:Number):Boolean{
		//checks if the comma is outside string closure
		var res:Boolean = false;
		var countBef:Number = 0, countAft:Number = 0;
		for(var i=0;i<index;i++){
			if(str.charAt(i) == "\"") countBef++;
		}
		for(var i=index+1;i<str.length;i++){
			if(str.charAt(i) == "\"") countAft++;
		}
		if(countBef%2==0 && countAft%2==0){
			//the quote count before and after the index is even, means it's outside
			return true;
		}
		else if(countBef%2!=0 && countAft%2!=0){
			//odd, it's inside
			return false;
		}else{
			throw new Error("String closure in the Json is not correct");
		}
	}
	function get su(){
		return StringU;
	}
}
}